/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cubelabs.nexus.controller;

import cubelabs.nexus.domain.BaseResponse;
import cubelabs.nexus.domain.FileUploadResponseDTO;
import cubelabs.nexus.domain.NexusFileUpload;
import cubelabs.nexus.domain.OrganizationDetailsDto;
import cubelabs.nexus.domain.OrganizationDto;
import cubelabs.nexus.domain.OrganizationRequestDto;
import cubelabs.nexus.domain.UserDTO;
import cubelabs.nexus.entity.Organization;
import cubelabs.nexus.entity.User;
import cubelabs.nexus.service.NexusService;
import cubelabs.nexus.service.OrganizationService;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 *
 * @author chaitanya
 */
@Controller
public class OrganizationController {

    @Autowired
    private NexusService nexusService;
    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "organizations", method = RequestMethod.POST)
    public @ResponseBody
    String createOrUpdateOrganization(@RequestBody OrganizationDto organizationDto, HttpServletRequest request) throws Exception {

        User user = (User) request.getSession().getAttribute("user");
        System.out.println(" organization creation in process ");
        System.out.println(" user : " + user.getId());
        //organizationDto.setUser(user);
        organizationService.createorUpdateOrganization(organizationDto, user);
        return "success";
    }
    
    @RequestMapping(value = "organization/{orgId}/setLogo", method = RequestMethod.POST)
    public @ResponseBody
    OrganizationDetailsDto uploadLogo(@PathVariable("orgId") Long orgId, HttpServletRequest request) throws Exception {
    	 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
         Iterator<String> itr =  multipartRequest.getFileNames();
         MultipartFile mpf = null;
 
         List<FileUploadResponseDTO> fileUploadResponseDTOList = new ArrayList<FileUploadResponseDTO>();

         while(itr.hasNext()){
             //2.1 get next MultipartFile
             mpf = multipartRequest.getFile(itr.next());
             NexusFileUpload nexusFileUpload = new NexusFileUpload();
             nexusFileUpload.setMultiPartFile(mpf);
             
             FileUploadResponseDTO fileUploadResponseDTO = organizationService.uploadOrgLogoToAmazon(nexusFileUpload, orgId);

             fileUploadResponseDTOList.add(fileUploadResponseDTO);
         }
         return organizationService.getOrganization(orgId);
    }
    
    
    @RequestMapping(value = "organization/{orgId}", method = RequestMethod.GET)
    public @ResponseBody
    OrganizationDetailsDto getOrganization( HttpServletRequest request,@PathVariable("orgId") Long orgId) throws Exception {
        return organizationService.getOrganization(orgId);
    }

    @RequestMapping(value = "organization/{orgId}/experts", method = RequestMethod.GET)
    public @ResponseBody
    List<UserDTO> getExperts(@RequestBody OrganizationDto organizationDto, HttpServletRequest request,@PathVariable("orgId") Long orgId) throws Exception {
        return organizationService.getExperts(orgId);
    }
        
    /**
     * Simply selects the list of organizations for user.
     */
    @RequestMapping(value = "/organizations", method = RequestMethod.GET)
    public @ResponseBody
    List<OrganizationDto> getOrganizationsForUser(
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        List<OrganizationDto> organizationsList = organizationService.findOrganizationsByUser(user);
        return organizationsList;
    }

    @RequestMapping(value = "/organization/{orgId}/join", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse joinOrganization(
            HttpServletRequest request, HttpServletResponse response, @PathVariable("orgId") Long orgId, @RequestBody OrganizationRequestDto orgRequestDto)
            throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        return organizationService.joinOrganization(orgId, user.getId(), orgRequestDto.getMessage());

    }
    
    @RequestMapping(value = "/organization/{orgId}/request", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse requestExpert(
            HttpServletRequest request, HttpServletResponse response, @PathVariable("orgId") Long orgId, @RequestBody OrganizationRequestDto orgRequestDto)
            throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        return organizationService.inviteExpert(orgId, orgRequestDto.getUserId(),user.getId(), orgRequestDto.getMessage());
    }
    
    @RequestMapping(value = "/organization/{orgId}/request", method = RequestMethod.GET)
    public @ResponseBody
    List<OrganizationRequestDto> pendingRequests(
            HttpServletRequest request, HttpServletResponse response, @PathVariable("orgId") Long orgId)
            throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        return organizationService.getExpertRequests(user.getId(), orgId);
    }
    
    @RequestMapping(value = "/organizations/myrequests", method = RequestMethod.GET)
    public @ResponseBody
    List<OrganizationRequestDto> orgPendingRequests(
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        return organizationService.getOrgRequests(user.getId());
    }
    
    
   @RequestMapping(value = "/organization/{orgId}/request/{requestId}", method = RequestMethod.GET)
    public @ResponseBody
    Boolean acceptExpert(HttpServletRequest request, HttpServletResponse response, 
        @PathVariable("orgId") Long orgId, @PathVariable("requestId") Long requestId)
            throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        return organizationService.acceptExpertToJoin(requestId, user.getId());
    }
   
    @RequestMapping(value = "/user/{userId}/request/{requestId}", method = RequestMethod.GET)
    public @ResponseBody
    Boolean acceptOrgRequest(HttpServletRequest request, HttpServletResponse response, 
        @PathVariable("userId") Long userId, @PathVariable("requestId") Long requestId)
            throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        return organizationService.acceptOrgnizationRequest(requestId, user.getId());
    }
}
